module.exports   A
last analyzed

Complexity

Conditions 3
Paths 8

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 23
ccs 10
cts 11
cp 0.9091
crap 3.0067
rs 9.0856
1 1
var jwt = require('jsonwebtoken');
2
3 1
module.exports = function(secret) {
4 1
    return function(req, res, next) {
5
        let token;
6 3
        if (req.headers.authorization) {
7
            token = req.headers['authorization'].split(' ')[1];
8
        } else {
9 3
            token = req.body.token || req.query.token || req.headers['x-access-token'];
10
        }
11 3
        if (token) {
12 2
            jwt.verify(token, secret, function(err, decoded) {
13 2
                if (err) {
14 1
                    return res.status(403).send({ success: false, message: 'Failed to authenticate token.' });
15
                } else {
16 1
                    req.decoded = decoded;
17 1
                    next();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
18
                }
19
            });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
20
        } else {
21 1
            return res.status(403).send({
22
                success: false,
23
                message: 'No token provided.'
24
            });
25
        }
26
    }
27
}
28